package com.huabo.interceptor;

import com.huabo.utils.AjaxUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 对Spring MVC全局异常拦截
 * Created by Jackie Liu on 2017/6/30.
 */
public class ViewExceptionHandler implements HandlerExceptionResolver {

    private static final Logger log = Logger.getLogger(ViewExceptionHandler.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        log.error("Spring MVC捕获异常信息", e);
        String url = request.getRequestURI();
        if (url.endsWith(".json")) {
            AjaxUtils.ajaxJsonErrorMessage(response, "系统内部异常，请联系管理员");
            return new ModelAndView();
        }

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("message", "系统内部异常，请联系管理员");

        return new ModelAndView("error", resultMap);
    }
}
